<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-pipes.html" />
<link rel="prev" href="module-pty.html" />
<link rel="parent" href="unix.html" />
<link rel="next" href="module-pipes.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>16.10 fcntl -- The fcntl() and ioctl() system calls</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="16.9 pty  "
  href="module-pty.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="16. unix Specific Services"
  href="unix.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="16.11 pipes  "
  href="module-pipes.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-pty.html">16.9 pty  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="unix.html">16. Unix Specific Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-pipes.html">16.11 pipes  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00181000000000000000000">
16.10 <tt class="module">fcntl</tt> --
         The <tt class="function">fcntl()</tt> and <tt class="function">ioctl()</tt> system calls</a>
</h1>

<p>
<a name="module-fcntl"></a>  
<p class="availability">Availability: <span
 class="platform">Unix</span>.</p>

<p>
<a id='l2h-3559' xml:id='l2h-3559'></a><a id='l2h-3560' xml:id='l2h-3560'></a>
<p>
This module performs file control and I/O control on file descriptors.
It is an interface to the <tt class="cfunction">fcntl()</tt> and <tt class="cfunction">ioctl()</tt>
<span class="Unix">Unix</span> routines.

<p>
All functions in this module take a file descriptor <var>fd</var> as their
first argument.  This can be an integer file descriptor, such as
returned by <code>sys.stdin.fileno()</code>, or a file object, such as
<code>sys.stdin</code> itself, which provides a <tt class="method">fileno()</tt> which
returns a genuine file descriptor.

<p>
The module defines the following functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3561' xml:id='l2h-3561' class="function">fcntl</tt></b>(</nobr></td>
  <td><var>fd, op</var><big>[</big><var>, arg</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Perform the requested operation on file descriptor <var>fd</var> (file
  objects providing a <tt class="method">fileno()</tt> method are accepted as well).
  The operation is defined by <var>op</var> and is operating system
  dependent.  These codes are also found in the <tt class="module">fcntl</tt>
  module. The argument <var>arg</var> is optional, and defaults to the
  integer value <code>0</code>.  When present, it can either be an integer
  value, or a string.  With the argument missing or an integer value,
  the return value of this function is the integer return value of the
  C <tt class="cfunction">fcntl()</tt> call.  When the argument is a string it
  represents a binary structure, e.g. created by
  <tt class="function"><tt class="module"><a href="module-struct.html">struct</a></tt>.pack()</tt>. The binary data is copied to a buffer
  whose address is passed to the C <tt class="cfunction">fcntl()</tt> call.  The
  return value after a successful call is the contents of the buffer,
  converted to a string object.  The length of the returned string
  will be the same as the length of the <var>arg</var> argument.  This is
  limited to 1024 bytes.  If the information returned in the buffer by
  the operating system is larger than 1024 bytes, this is most likely
  to result in a segmentation violation or a more subtle data
  corruption.

<p>
If the <tt class="cfunction">fcntl()</tt> fails, an <tt class="exception">IOError</tt> is
  raised.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3562' xml:id='l2h-3562' class="function">ioctl</tt></b>(</nobr></td>
  <td><var>fd, op</var><big>[</big><var>, arg</var><big>[</big><var>, mutate_flag</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  This function is identical to the <tt class="function">fcntl()</tt> function,
  except that the operations are typically defined in the library
  module <tt class="module"><a href="module-termios.html">termios</a></tt> and the argument handling is even more
  complicated.

<p>
The parameter <var>arg</var> can be one of an integer, absent (treated
  identically to the integer <code>0</code>), an object supporting the
  read-only buffer interface (most likely a plain Python string) or an
  object supporting the read-write buffer interface.

<p>
In all but the last case, behaviour is as for the <tt class="function">fcntl()</tt>
  function.

<p>
If a mutable buffer is passed, then the behaviour is determined by
  the value of the <var>mutate_flag</var> parameter.

<p>
If it is false, the buffer's mutability is ignored and behaviour is
  as for a read-only buffer, except that the 1024 byte limit mentioned
  above is avoided - so long as the buffer you pass is as least as
  long as what the operating system wants to put there, things should
  work.

<p>
If <var>mutate_flag</var> is true, then the buffer is (in effect) passed
  to the underlying <tt class="function">ioctl()</tt> system call, the latter's
  return code is passed back to the calling Python, and the buffer's
  new contents reflect the action of the <tt class="function">ioctl()</tt>.  This is a
  slight simplification, because if the supplied buffer is less than
  1024 bytes long it is first copied into a static buffer 1024 bytes
  long which is then passed to <tt class="function">ioctl()</tt> and copied back into
  the supplied buffer.

<p>
If <var>mutate_flag</var> is not supplied, then from Python 2.5 it
  defaults to true, which is a change from versions 2.3 and 2.4.
  Supply the argument explicitly if version portability is a priority.

<p>
An example:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; import array, fcntl, struct, termios, os
&gt;&gt;&gt; os.getpgrp()
13341
&gt;&gt;&gt; struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, "  "))[0]
13341
&gt;&gt;&gt; buf = array.array('h', [0])
&gt;&gt;&gt; fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
0
&gt;&gt;&gt; buf
array('h', [13341])
</pre></div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3563' xml:id='l2h-3563' class="function">flock</tt></b>(</nobr></td>
  <td><var>fd, op</var>)</td></tr></table></dt>
<dd>
Perform the lock operation <var>op</var> on file descriptor <var>fd</var> (file
  objects providing a <tt class="method">fileno()</tt> method are accepted as well).
See the <span class="Unix">Unix</span> manual <span class="manpage"><i>flock</i>(3)</span> for details.  (On some
systems, this function is emulated using <tt class="cfunction">fcntl()</tt>.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3564' xml:id='l2h-3564' class="function">lockf</tt></b>(</nobr></td>
  <td><var>fd, operation,
    </var><big>[</big><var>length, </var><big>[</big><var>start, </var><big>[</big><var>whence</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This is essentially a wrapper around the <tt class="function">fcntl()</tt> locking
calls.  <var>fd</var> is the file descriptor of the file to lock or unlock,
and <var>operation</var> is one of the following values:

<p>

<ul>
<li><tt class="constant">LOCK_UN</tt> - unlock
</li>
<li><tt class="constant">LOCK_SH</tt> - acquire a shared lock
</li>
<li><tt class="constant">LOCK_EX</tt> - acquire an exclusive lock
</li>
</ul>

<p>
When <var>operation</var> is <tt class="constant">LOCK_SH</tt> or <tt class="constant">LOCK_EX</tt>, it
can also be bit-wise OR'd with <tt class="constant">LOCK_NB</tt> to avoid blocking on
lock acquisition.  If <tt class="constant">LOCK_NB</tt> is used and the lock cannot
be acquired, an <tt class="exception">IOError</tt> will be raised and the exception
will have an <var>errno</var> attribute set to <tt class="constant">EACCES</tt> or
<tt class="constant">EAGAIN</tt> (depending on the operating system; for portability,
check for both values).  On at least some systems, <tt class="constant">LOCK_EX</tt>
can only be used if the file descriptor refers to a file opened for
writing.

<p>
<var>length</var> is the number of bytes to lock, <var>start</var> is the byte
offset at which the lock starts, relative to <var>whence</var>, and
<var>whence</var> is as with <tt class="function">fileobj.seek()</tt>, specifically:

<p>

<ul>
<li><tt class="constant">0</tt> - relative to the start of the file
      (<tt class="constant">SEEK_SET</tt>)
</li>
<li><tt class="constant">1</tt> - relative to the current buffer position
      (<tt class="constant">SEEK_CUR</tt>)
</li>
<li><tt class="constant">2</tt> - relative to the end of the file
      (<tt class="constant">SEEK_END</tt>)
</li>
</ul>

<p>
The default for <var>start</var> is 0, which means to start at the
beginning of the file.  The default for <var>length</var> is 0 which means
to lock to the end of the file.  The default for <var>whence</var> is also
0.
</dl>

<p>
Examples (all on a SVR4 compliant system):

<p>
<div class="verbatim"><pre>
import struct, fcntl, os

f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)

lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
</pre></div>

<p>
Note that in the first example the return value variable <var>rv</var> will
hold an integer value; in the second example it will hold a string
value.  The structure lay-out for the <var>lockdata</var> variable is
system dependent -- therefore using the <tt class="function">flock()</tt> call may be
better.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-os.html">os</a></tt>:</b>
    <dd>If the locking flags <tt class="constant">O_SHLOCK</tt> and
		 <tt class="constant">O_EXLOCK</tt> are present in the <tt class="module">os</tt> module,
  		 the <tt class="function">os.open()</tt> function provides a more
  		 platform-independent alternative to the <tt class="function">lockf()</tt>
  		 and <tt class="function">flock()</tt> functions.
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="16.9 pty  "
  href="module-pty.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="16. unix Specific Services"
  href="unix.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="16.11 pipes  "
  href="module-pipes.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-pty.html">16.9 pty  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="unix.html">16. Unix Specific Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-pipes.html">16.11 pipes  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
